package info.sean.inerface.impl;

import info.sean.inerface.DataSecurityService;
import info.sean.logger.GmfLogger;
import org.slf4j.Logger;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.NoSuchAlgorithmException;
import java.sql.Date;

import static info.sean.util.AES.byteToHexString;

public class DataSecurityServiceImpl implements DataSecurityService {

    private static Logger logger = GmfLogger.getLogger(DataSecurityServiceImpl.class);
    // SoftZek 使用
    private static final String AES_ECB_PKCS5PADDING = "AES/ECB/PKCS5Padding";


    public static void main(String[] args) {

        String algorithm = AES_ECB_PKCS5PADDING.substring(0, AES_ECB_PKCS5PADDING.indexOf("/"));
        logger.error("algorithm::"+algorithm);
        System.out.println(algorithm);
        String getkey = getkey(algorithm);
        logger.error("加密的秘钥值为："+getkey);
        System.out.println(getkey);
    }
    /**
     *
     * @param s 秘钥类型 AES_ECB_PKCS5PADDING
     * @return 相应的秘钥值
     */
    public static String getkey(String s) {
        String ss = null;
        try {
            KeyGenerator kg = KeyGenerator.getInstance(s);
            kg.init(128);//要生成多少位，只需要修改这里即可128, 192或256
            SecretKey sk = kg.generateKey();
            byte[] b = sk.getEncoded();
            ss = byteToHexString(b);
        } catch (NoSuchAlgorithmException e) {
            logger.error("NoSuchAlgorithmException", e);
        }
        return ss;
    }



}
